package cn.jinjimi.app.main.widget.biz;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.jinjimi.base.util.StringUtils;
import cn.jinjimi.framework.helper.annotation.EventTrace;
import cn.jinjimi.framework.helper.annotation.EventTraceSelector;
import cn.jinjimi.app.AccountManager;
import cn.jinjimi.app.R;
import cn.jinjimi.app.bean.EventDataField;
import cn.jinjimi.app.bean.EventID;
import cn.jinjimi.app.entity.Account;
import cn.jinjimi.app.entity.MemberStatistics;
import cn.jinjimi.app.modules.drp.Drp;
import cn.jinjimi.framework.router.Router;
import cn.jinjimi.app.bean.RouterConstants;
import cn.jinjimi.app.main.activity.LoginActivity;
import cn.jinjimi.app.main.widget.FrescoImageView.FrescoImageView;
import cn.jinjimi.app.main.widget.ShadowLayout;

/**
 * Created by Roy
 * 我界面  头部
 * Date: 16/5/13
 */
public class InfoHeadView extends LinearLayout implements View.OnClickListener {

    public final static int TypeUnLogin = 0;
    public final static int TypeNoPromotion = 1;
    public final static int TypeHavePromotion = 2;

    private final Drp.Config mDrpConfig;
    private MemberStatistics mMemberStatistics;

    private Button btnLogin;
    private ShadowLayout rlBoard;
    private FrescoImageView imgHeaderBg, imgBoardBg, imgAvatar, imgLevel, imgMemberDetail;
    private TextView tvGrowUpPoints, tvDescription;
    private TextView tvExpectCommission;
    private TextView tvWalletMoney;
    private TextView tvSumCommission;
    private TextView tvName;

    public InfoHeadView(Context context) {
        super(context);
        LayoutInflater.from(getContext()).inflate(R.layout.fragment_info_header_view, this);
        initView();
        initListener();
        onRefresh();

        mDrpConfig = Drp.getInstance().getConfig();
    }

    private void initView() {
        btnLogin = (Button) findViewById(R.id.btn_login);
        rlBoard = (ShadowLayout) findViewById(R.id.rl_board);
        imgHeaderBg = (FrescoImageView) findViewById(R.id.iv_header_bg);
        imgBoardBg = (FrescoImageView) findViewById(R.id.iv_board_bg);
        imgAvatar = (FrescoImageView) findViewById(R.id.img_avatar);
        imgLevel = (FrescoImageView) findViewById(R.id.img_level);
        imgMemberDetail = (FrescoImageView) findViewById(R.id.iv_member_detail);
        tvName = (TextView) findViewById(R.id.tv_user_name);
        tvGrowUpPoints = (TextView) findViewById(R.id.tv_grow_up_point);
        tvDescription = (TextView) findViewById(R.id.tv_member_desc);

        tvExpectCommission = (TextView) findViewById(R.id.tv_expect_commission);
        tvWalletMoney = (TextView) findViewById(R.id.tv_wallet_money);
        tvSumCommission = (TextView) findViewById(R.id.tv_sum_commission);

        imgHeaderBg.loadView("", R.drawable.shape_orange_ffb096);
        imgBoardBg.loadView("", R.drawable.shape_orange_cor10_ff6430);
    }

    private void initListener() {
        btnLogin.setOnClickListener(this);
        imgAvatar.setOnClickListener(this);
        imgMemberDetail.setOnClickListener(this);
        findViewById(R.id.wallet_except).setOnClickListener(this);
        findViewById(R.id.wallet_money).setOnClickListener(this);
        findViewById(R.id.wallet_sum).setOnClickListener(this);
    }

    /* 读取缓存数据 */
    public void onRefresh() {
        setUserInfo(AccountManager.getInstance().isLogin());
    }

    public void setData(MemberStatistics member) {
        setUserInfo(AccountManager.getInstance().isLogin());
        setMemberInfo(member);
    }

    public void setUserInfo(boolean isLogin) {
        btnLogin.setVisibility(isLogin ? GONE : VISIBLE);
        rlBoard.setVisibility(isLogin ? VISIBLE : GONE);
        if (!isLogin) {
            imgHeaderBg.loadView("", R.drawable.shape_orange_ff6430);
        }
        Account account = AccountManager.getInstance().getAccount();
        boolean hasAccount = account != null;
        tvName.setText(hasAccount ? account.realName : "");
        imgAvatar.loadView(hasAccount && StringUtils.isNotEmpty(account.avatar) ? account.avatar : "", R.drawable.logo);
    }

    public void setMemberInfo(MemberStatistics data) {
        if (data == null) {
            imgHeaderBg.loadView("", R.drawable.shape_orange_ffb096);
            imgBoardBg.loadView("", R.drawable.shape_orange_cor10_ff6430);
            tvGrowUpPoints.setText(R.string.me_member_no_data);
            imgLevel.setVisibility(GONE);
            imgMemberDetail.setVisibility(GONE);
            tvDescription.setVisibility(GONE);
            return;
        }
        mMemberStatistics = data;
        MemberStatistics.Attachments attachments = data.attachments;
        boolean hasBackground = attachments != null && StringUtils.isNotEmpty(attachments.background);
        imgHeaderBg.loadView(hasBackground ? attachments.background : "", R.drawable.shape_orange_ffb096);
        boolean hasBoard = attachments != null && StringUtils.isNotEmpty(attachments.board);
        imgBoardBg.loadView(hasBoard ? attachments.board : "", R.drawable.shape_orange_cor10_ff6430);
        boolean hasLevelIcon = attachments != null && StringUtils.isNotEmpty(attachments.levelIcon);
        imgLevel.setVisibility(hasLevelIcon ? VISIBLE : GONE);
        imgLevel.loadView(hasLevelIcon ? attachments.levelIcon : "");
        boolean hasLinkIcon = attachments != null && StringUtils.isNotEmpty(attachments.linkIcon);
        imgMemberDetail.loadView(hasLinkIcon ? attachments.linkIcon : "");
        imgMemberDetail.setVisibility(hasLinkIcon ? VISIBLE : GONE);

        MemberStatistics.Colors colors = data.colors;
        boolean hasColors = colors != null && colors.shadow != null;
        rlBoard.setShadowColor(hasColors ? Color.parseColor(getColor(colors.shadow)) : getResources()
                .getColor(R.color.transparent));

        imgAvatar.loadView(StringUtils.isEmpty(data.avatarUrl) ? "" : data.avatarUrl, R.drawable.logo);
        tvGrowUpPoints.setText("成长值：" + data.point);
        tvDescription.setVisibility(VISIBLE);
        tvDescription.setText(data.nextLevelDescr);
    }

    private String getColor(String color) {
        return color.replace("0x", "#ad");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login: //立即登录
                Router.doLogin(getContext());
                break;
            case R.id.img_avatar: //个人资料
                openUserInfo();
                break;
            case R.id.iv_member_detail://会员详情
                if (mMemberStatistics != null && AccountManager.getInstance().isLogin()) {
                    openMember();
                }
                break;
            case R.id.wallet_except://待生效
                openWalletExcept(AccountManager.getInstance().isLogin());
                break;
            case R.id.wallet_money://可提现
                openWalletMoney(AccountManager.getInstance().isLogin());
                break;
            case R.id.wallet_sum://累计
                openWalletSum(AccountManager.getInstance().isLogin());
                break;
        }
    }

    @EventTrace(value = EventID.APP_SUPER_MEMBER_CLICK, paramsK = {EventDataField.ParamFrom}, paramsV = {EventID.FROM_APP_OWNER})
    private void openMember() {
        Router.create(getContext(), mMemberStatistics.linkUrl).open();
    }

    /* 待生效 */
    @EventTrace({EventID.APP_OWNER_EXECUTORY, EventID.APP_OWNER_LOGIN_IMMEDIATELY})
    private void openWalletExcept(@EventTraceSelector boolean isLogin) {
        if (mDrpConfig != null && !TextUtils.isEmpty(mDrpConfig.uneffectiveProfitUrl)) {
            if (!isLogin) {
                setLoginSource();
            }
            Router.create(getContext(), mDrpConfig.uneffectiveProfitUrl).fourceLogin(true).open();
        }
    }

    /* 可提现界面 */
    @EventTrace({EventID.APP_OWNER_WITHDRAWABLE, EventID.APP_OWNER_LOGIN_IMMEDIATELY})
    private void openWalletMoney(@EventTraceSelector boolean isLogin) {
        if (mDrpConfig != null && !TextUtils.isEmpty(mDrpConfig.transfer)) {
            if (!isLogin) {
                setLoginSource();
            }
            Router.create(getContext(), mDrpConfig.transfer).fourceLogin(true).open();
        }
    }

    /* 累计 推广费明细 */
    @EventTrace({EventID.APP_OWNER_ADD_UP, EventID.APP_OWNER_LOGIN_IMMEDIATELY})
    private void openWalletSum(@EventTraceSelector boolean isLogin) {
        if (mDrpConfig != null && !TextUtils.isEmpty(mDrpConfig.accumulatedProfit)) {
            if (!isLogin) {
                setLoginSource();
            }
            Router.create(getContext(), mDrpConfig.accumulatedProfit).fourceLogin(true).open();
        }
    }

    /* 登陆统计 */
    public void setLoginSource() {
        LoginActivity.setLoginFrom(EventID.APP_OWNER_LOGIN_IMMEDIATELY);
    }

    /* 打开个人资料 */
    @EventTrace(value = EventID.APP_OWNER_LIST, paramsK = {EventDataField.ParamId}, paramsV = {"个人资料"})
    public void openUserInfo() {
        Router.create(getContext(), RouterConstants.getNativeRouter(RouterConstants.MeProfile)).open();
    }

}
